//
//  LoginViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/17.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "LoginViewController.h"
#import "UserDTO.h"

@interface LoginViewController ()

@property (nonatomic, strong) UIImageView * logoView;
@property (nonatomic, strong) UIImageView * logoLabel;
@property (nonatomic, strong) UITextField * accountTF;
@property (nonatomic, strong) UITextField * passwordTF;
@property (nonatomic, strong) UIButton    * loginButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}


-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [self.view endEditing:YES];
}

- (void)setupUI{

    self.title = @"登录";
    
    /*!
     *  添加
     */
    [self.view addSubview:self.logoView];
    [self.view addSubview:self.logoLabel];
    [self.view addSubview:self.accountTF];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.loginButton];
    
    /*!
     *  设置布局
     */
    
    CGFloat padding = (SCREEN_WIDTH==320) ? 10 : 70;
    
    [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(64+padding);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(70, 90));
    }];
    
    [self.logoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.logoView.mas_bottom).offset(2);
        make.centerX.equalTo(self.logoView);
        make.size.mas_equalTo(CGSizeMake(70, 45));
    }];
    
    [self.accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.logoLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(35);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.accountTF.mas_bottom).offset(20);
        make.height.mas_equalTo(35);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.passwordTF.mas_bottom).offset(30);
        make.height.mas_equalTo(45);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
}

#pragma mark  - Actions
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}


- (void)clickLogin:(UIButton *)btn{

    NSString *account  = self.accountTF.text;
    
    NSString *password = self.passwordTF.text;
    
    if ([account isEqualToString:@""] || [password isEqualToString:@""]) {
        
        [SVPAlertManager alertErrorStatus:@"请填写全账户和密码"];
        return;
    }
    
    //这个账户和密码都是虚假的，带大家自己判断即可
    if ([account isEqualToString:@"37"] || [password isEqualToString:@"37"]) {
    
        WEAKSELF
        [[NetWorkManager sharedManager] loginUserWihtAccount:account password:password successBlock:^(UserDTO *dto){
            
            loggerWarn(@"name = %@",dto.true_name);
            
            [MLDefaults setBool:YES forKey:kIsUserLoginKey];
            
            //发送个通知
            [[NSNotificationCenter defaultCenter] postNotificationName:kIsUserLoginNotificationKey object:self];
            
            [SVPAlertManager alertSuccessStatus:@"登录成功"];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        } errorBlock:^(NSString *error) {
            
            [SVPAlertManager alertErrorStatus:error];
            [MLDefaults setBool:NO forKey:kIsUserLoginKey];
        }];
    }
    
    

}

#pragma mark  - Lazy
- (UIImageView *)logoView{

    if (_logoView == nil) {
        _logoView = [[UIImageView alloc] init];
        _logoView.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    }
    return _logoView;
}

- (UIImageView *)logoLabel{
    
    if (_logoLabel == nil) {
        _logoLabel = [[UIImageView alloc] init];
        _logoLabel.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    }
    return _logoLabel;
}


- (UITextField *)accountTF{
    if (_accountTF == nil) {
        _accountTF = [[UITextField alloc] init];
        _accountTF.placeholder = @"请输入账户";
        _accountTF.text = @"37";
        _accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountTF.borderStyle = UITextBorderStyleRoundedRect;

    }
    return _accountTF;
}

- (UITextField *)passwordTF{
    if (_passwordTF == nil) {
        _passwordTF = [[UITextField alloc] init];
        _passwordTF.placeholder = @"请输入密码";
        _passwordTF.text = @"37";

        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTF.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _passwordTF;
}

-(UIButton *)loginButton{

    if (_loginButton == nil) {
        _loginButton = [[UIButton alloc] init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(clickLogin:) forControlEvents:UIControlEventTouchUpInside];
        _loginButton.backgroundColor = kThemeColor;
        _loginButton.titleLabel.font = FONT(20);
    }
    return _loginButton;
}

@end
